<template>
    <el-upload class="upload-demo" drag action="http://goapi.sanjitian.cn/api/files" :headers="headers"
        :show-file-list="false" :on-success="handleAvatarSuccess" multiple>
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">
            Drop file here or <em>click to upload</em>
        </div>
        <template #tip>
            <div class="el-upload__tip">
                jpg/png files with a size less than 500kb
            </div>
        </template>
    </el-upload>
    <div class="my-img-list">
        <div v-for="item in myImgList" :key="item.id" class="img-container">
            <img :src="item.filePathCos" :alt="item.fileType" />
            <el-icon class="close-btn" @click="removeImage(item)">
                <close />
            </el-icon>
        </div>
    </div>
</template>

<script setup lang="ts">
import { UploadFilled } from '@element-plus/icons-vue'
import { type UploadProps } from 'element-plus'
import { ref } from 'vue'

interface IFileInfo {
    fileExtension: string
    filePathCos: string
    fileSize: string
    fileType: string
    fileUse: string
    id: number
    mineType: string
    uploadTime: string
}

const myImgList = ref<IFileInfo[]>([])
const headers = {
    Authorization: `${localStorage.getItem('token')}`
}

const removeImage = (item: IFileInfo) => {
    myImgList.value = myImgList.value.filter(i => i.id !== item.id)
}

const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
    console.log(uploadFile, '#uploadFile')
    console.log(response.data, '#response')
    myImgList.value.push(response.data)
}


</script>
<style lang="scss" scoped>
.my-img-list {
    width: 100%;
    height: 200px;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;

    .img-container {
        position: relative;
        display: inline-block;
        overflow: hidden;
    }

    img {
        height: 150px;
        width: 150px;
    }

    .close-btn {
        font-size: 20px;
        position: absolute;
        top: 5px;
        right: 5px;
        cursor: pointer;
        color: #f56c6c;
        opacity: 0;
        transition: opacity 0.3s;
    }

    .img-container:hover .close-btn {
        opacity: 1;
    }
}
</style>